import { useUserStore } from "@/store"

// 请求拦截
export default function requestInterceptor(option: httpDefaultOption): any{
	// 1、先从全局存储中获取用户Token信息
	const userStore = useUserStore()
	const { token } = userStore
	// 2、判断当前请求是否需要Token且用户是否登录
	if(option.needToken&&!token){
		throw Error("您还未登录！")
	}
	// 3、设置请求头
	const headers:Record<string, string> = {}
	headers["Content-Type"] = "application/json"
	if(token){
		headers["X-Access-Token"] = token
	}
	if(option.isLoading){
		uni.showLoading({
			title: option.loadingTitle,
			mask: true
		})
	}
	return {
		url: option.baseUrl + option.url,
		method: option.method,
		data: option.data,
		header: Object.assign({},headers,option.header),
		timeout: option.timeout,
		dataType: option.dataType,
		responseType: option.responseType
	}
}